<template>
    <div>
        <!-- 保全申请信息:fzBqsqxx-->
        <div class="w_100 headTips d_f j_c_s_b">
            <div class="d_f a_i_c">
                <div class="line"></div>
                {{ name }}
            </div>
            <div class="isClose" @click="close">
                <el-icon v-if="type == 'smallForm'">
                    <CloseBold />
                </el-icon>
            </div>
            <el-switch v-if="type == 'bigForm'" @change="change" v-model="status"
                style="--el-switch-on-color: #13ce66;" />
            <el-button type="primary" v-if="type == 'useForm' && isReset == 1" @click="clearForm">清除表单</el-button>
        </div>
        <div class="formBox fzBqsqxx">
            <div class="fzBqsqxx_form">
                <div class="d_f a_i_c j_c_s_b m_t_20">
                    <div style="width: 30%;" class="label">跟踪信息</div>
                    <div style="width: 30%;" class="size">录入人</div>
                    <div style="width: 30%;" class="size">录入时间</div>
                    <div style="width: 26px;"></div>
                </div>
                <div v-for="(item, index) in model.assetPreservationBaseData.assetPreservationBaseData" class="d_f m_t_20 a_i_c j_c_s_b" :key="index">
                    <div style="width: 30%;">
                        <el-input v-model="item.tracking" :disabled="isAudit" placeholder="请输入跟踪信息" />
                    </div>
                    <div style="width: 30%;">
                        <el-input v-model="item.entrant" :disabled="isAudit" placeholder="请输入录入人" />
                    </div>
                    <div style="width: 30%;">

                        <el-date-picker clearable :disabled="isAudit" v-model="item.loadTime" placeholder="请选择录入时间"
                            value-format="YYYY-MM-DD" format="YYYY-MM-DD" />
                    </div>
                    <div style="width: 26px;" class="d_f a_i_c">
                        <img src="../../assets/reduce.png" alt="" @click="reduce(index)" class="reduce"
                            v-if="index > 0">
                    </div>
                </div>
                <div class="d_f a_i_c j_c_s_b m_t_20">
                    <div style="width: 30%;"></div>
                    <div style="width: 30%;"></div>
                    <div style="width: 30%;"></div>
                    <div style="width: 26px;" @click="add">
                        <img src="../../assets/add.png" alt="" class="reduce">
                    </div>
                </div>
            </div>

            <div class="segment" v-if="fields.length > 0"></div>
            <div class="formArrow d_f a_i_c m_t_30" v-if="fields.length > 0">
                <img src="../../assets/formArrow.png" alt="">
                <span>{{ subTitle || '新加字段' }}</span>
            </div>
            <div v-if="fields.length > 0">
                <myform :fields="fields" :model="model.assetPreservationBaseData" :options="options" class="m_t_16">
                </myform>
            </div>
        </div>
    </div>
</template>

<script lang='ts'>
import { defineComponent, reactive, toRefs, onBeforeMount } from 'vue'
import { fzBqsqxxType } from './data'
import { copy } from '../../utils/date'
import { formGet, unitFormSet } from '../../api/system/flowPath'
import { listByParentArr } from '../../api/login'
import { Message } from '../../utils/resetMessage'
import { useRouter } from 'vue-router'
import { CloseBold } from '@element-plus/icons-vue'
const model = {
    assetPreservationBaseData:{
        assetPreservationBaseData: [],
    },
}
export default defineComponent({
    name: '',
    props: {
        type: {
            type: String,
            default: '',
        },
        unitId: {
            type: [String, Number],
            default: '',
        },
        isEnabled: {
            type: Number,
            default: '',
        },
        nodeObj: {
            type: Object,
            default: {}
        },
        vars: {
            type: Object,
            default: {}
        },
        isAudit: {
            type: Boolean,
            default: true
        },
    },
    components: {
        CloseBold
    },
    setup(props: any) {
        const data: fzBqsqxxType = reactive({
            model: copy(model),
            status: true,
            id: '',
            name: '保全申请信息',
            subTitle: '',
            options: {},
            fields: [],
            list: [],
            isAdd: false,
            isReset: 2,
        })
        const router = useRouter()
        onBeforeMount(() => {
            methods.getData()
            data.status = props.isEnabled == 0 ? false : true
        })
        const methods = {
            close() {
                router.push('/system/flowPath/childForm/index')
            },
            //获取自定义表单
            getData() {
                
                formGet({ tag: 'fzBqsqxx' }).then((res: any) => {
                    data.id = res.id
                    data.name = res.name
                    data.isReset = res.isReset
                    if (!res.formStruct || res.formStruct == 'null' || res.formStruct == '{}') return
                    data.subTitle = res.subTitle
                    let formStruct = JSON.parse(res.formStruct)
                    formStruct.formFields.map((item:any)=>{
                        data.model.assetPreservationBaseData[item.prop]=''
                    })
                    data.fields = formStruct.formFields
                    data.fields.map((item: any) => {
                        item.disabled = props.isAudit
                    })
                    if (props.vars) {
                        const localData = props.vars
                        if(localData.assetPreservationBaseData){
                            for (let key in data.model.assetPreservationBaseData) {
                            if( localData.assetPreservationBaseData[key]!=null){
                                data.model.assetPreservationBaseData[key] = localData.assetPreservationBaseData[key]
                            }
                        }
                            data.model.assetPreservationBaseData.assetPreservationBaseData=localData.assetPreservationBaseData.assetPreservationBaseData || []
                        }
                       data.isAdd= data.model.assetPreservationBaseData.assetPreservationBaseData.some((item:any)=>item.nodeId==props.nodeObj.id)
                    } 
                    let arr = formStruct.formFields.filter((item: any) => {
                        return item.type == 'select'
                    })
                    let ids = arr.map((item: any) => {
                        return item.prop
                    })
                    if (ids.length == 0) return
                    listByParentArr({ ids: ids }).then((res: any) => {
                        ids.map((item: any) => {
                            data.options[item] = res[item]
                        })
                    })
                })
            },
            //设置表单的启用跟停用
            change() {
                unitFormSet({ isEnabled: data.status ? 1 : 0, id: data.id, unitId: props.unitId }).then(() => {
                    Message({ type: 'success', message: '设置成功' })
                }).catch(() => {
                    data.status = !data.status
                })
            },
            add() {
                if (data.isAdd) return Message({ type: 'error', message: '每一次“资产保全”更新一次记录' })
                data.model.assetPreservationBaseData.assetPreservationBaseData.push({
                    tracking: '',//跟踪信息
                    entrant: '',//录入人
                    loadTime: '',//录入时间
                    nodeId:props.nodeObj.id
                })
                data.isAdd = true
            },
            reduce(index: number) {
                data.model.assetPreservationBaseData.assetPreservationBaseData.splice(index, 1)
                data.isAdd = false
            },
            //清除表单数据
            clearForm() {
                data.model = copy(model)
                if (data.isAdd) {
                    data.model.assetPreservationBaseData.assetPreservationBaseData = data.model.assetPreservationBaseData.assetPreservationBaseData.splice(data.list.length - 1, 1)
                }
            },
            resultData() {
                return new Promise((resolve, reject) => {
                    if (!data.isAdd) {
                        reject(false)
                        return Message({ type: 'error', message: '每一次“资产保全”更新一次记录' })
                    }
                   
                    resolve(data.model)
                })
            },
            saveData() {
                return new Promise((resolve, reject) => {
                    resolve(data.model)
                })
            },
        }
        return {
            ...toRefs(data),
            ...methods
        }
    }
})
</script>
<style scoped lang='scss'>
.vote {
    color: rgba(0, 0, 0, 0.9);
    font-size: 14px;
}

.num {
    color: rgba(0, 0, 0, 0.9);
    font-size: 36px;
    font-weight: 700;
}

.talk {
    width: 22px;
    height: 22px;
}

.reduce {
    width: 26px;
    height: 26px;
    cursor: pointer;
}

.label {
    color: rgba(0, 0, 0, 0.9);
    font-size: 14px;
}

.label::before {
    color: var(--el-color-danger);
    content: "*";
    margin-right: 4px;
}

.size {
    color: rgba(0, 0, 0, 0.9);
    font-size: 14px;
}
</style>
<style lang='scss'>
.fzBqsqxx {
    .el-form-item {
        width: 50% !important;
        margin-right: 0px !important;

        .el-form-item__content {
            width: calc(100% - 150px) !important;

            .el-input,
            .el-cascader,
            .el-select {
                width: 100% !important;
            }
        }

    }
}

.fzBqsqxx_form {

    .el-select,
    .el-input {
        width: calc(100% - 20px) !important;
    }
}
</style>